package Leetcode.ArrayAndLinkedList.mergeTwoLists21;

/**
 * 次数2
 */
public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null && l2 == null) {
            return null;
        }
        ListNode dummyHead = new ListNode(-1);
        ListNode result = dummyHead;
        while(l1 != null && l2 != null) {
            if(l1.val <= l2.val) {
                dummyHead.next = l1;
                dummyHead = dummyHead.next;
                l1 = l1.next;
                continue;
            }
            if(l1.val > l2.val) {
                dummyHead.next = l2;
                dummyHead = dummyHead.next;
                l2 = l2.next;
                continue;
            }
        }

        if(l1 == null) {
            dummyHead.next = l2;
            return result.next;
        }

        if(l2 == null) {
            dummyHead.next = l1;
            return result.next;
        }

        return null;
    }

    public static void main(String[] args) {
        int nums[] = {1,2,4,6};
        int nums2[] = {3,5};
        Solution solution = new Solution();
        ListNode node1 = new ListNode(nums);
        ListNode node2 = new ListNode(nums2);

        System.out.println(node2);
        System.out.println(solution.mergeTwoLists(node1, node2));
    }
}


/**
 public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
 if (l1 == null && l2 == null) {
 return null;
 }

 ListNode dummyHead = new ListNode(-1);
 ListNode result = dummyHead;
 while (l1 != null && l2 != null) {

 if (l1 != null && l2 != null && l1.val <= l2.val) {
 dummyHead.next = l1;
 l1 = l1.next;
 dummyHead.next.next = null;
 dummyHead = dummyHead.next;
 }
 if (l1 != null && l2 != null && l1.val > l2.val) {
 dummyHead.next = l2;
 l2 = l2.next;
 dummyHead.next.next = null;
 dummyHead = dummyHead.next;
 }
 }

 if (l1 == null) {
 dummyHead.next = l2;
 }
 if (l2 == null) {
 dummyHead.next = l2;
 }

 return result.next;
 }
 */